package com.example.zookeeper.rmi.service;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.concurrent.CountDownLatch;

public class Serviceprovider
{
    private ZooKeeper zooKeeper;

    private final String ZK_RMI_REGISTRY_PATH = "/rmi_registry";
    private final String ZK_RMI_PROVIDER_PATH = ZK_RMI_REGISTRY_PATH + "/provider";


    public void publish(String host, int port, Remote remote)
    {
        if (publishRmiService(host, port, remote))
        {
            ZooKeeper zooKeeper = getZookeeper();
            if (zooKeeper != null)
            {
                String url = String.format("rmi://%s:%s/%s", host, port, remote.getClass().getName());
                if (createNode(url))
                {
                    System.out.println(url + "发布成功！");
                }
            }
        }
    }

    public boolean publishRmiService(String host, int port, Remote remote)
    {
        try
        {
            String url = String.format("rmi://%s:%s/%s", host, port, remote.getClass().getName());
            LocateRegistry.createRegistry(port);
            Naming.rebind(url, remote);
            return true;
        } catch (RemoteException | MalformedURLException e)
        {
            e.printStackTrace();
        }

        return false;
    }

    private boolean createNode(String url)
    {
        try
        {
            byte[] data = url.getBytes();
            String path = zooKeeper.create(ZK_RMI_PROVIDER_PATH, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println("create node url: " + url);
            return true;
        } catch (KeeperException | InterruptedException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    private ZooKeeper getZookeeper()
    {
        if (zooKeeper == null)
        {
            synchronized (this)
            {
                if (zooKeeper == null)
                {
                    final CountDownLatch latch = new CountDownLatch(1);
                    ZooKeeper zooKeeper = null;
                    try
                    {
                        zooKeeper = new ZooKeeper("192.168.3.91:2181", 10 * 1000, watchedEvent ->
                        {
                            System.out.println("zookeeper 连接成功");
                            latch.countDown();
                        });
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    try
                    {
                        latch.await();
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    this.zooKeeper = zooKeeper;
                }
            }
        }
        return zooKeeper;
    }

}
